package main

import "fmt"

type ListNode struct {
	Val  int
	Next *ListNode
}

//双指针
func deleteNode(head *ListNode, val int) *ListNode {
	if head == nil { //链表和树的题目，首先就要想到空节点的处理
		return nil
	}
	l := new(ListNode) //做一个新节点，其next指向head
	l.Next = head
	pre, post := l, l.Next //双节点，一前一后
	for post != nil {      //遍历找要删除的节点
		if post.Val == val {
			pre.Next = post.Next //执行删除操作
			return l.Next
		}
		pre = post
		post = post.Next
	}
	return nil
}

func main() {
	s := &ListNode{
		Val: 7,
		Next: &ListNode{
			Val: 4,
			Next: &ListNode{
				Val:  5,
				Next: nil,
			},
		},
	}
	fmt.Println(deleteNode(s, 5))
}
